<template>

  <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="100px">
    <el-form-item label="企业名称" prop="tenantName">
      <el-input clearable :maxlength="50" v-model="queryParams.tenantName" placeholder="请输入企业名称或简称"
        @keyup.enter="handleQuery" class="!w-200px" />
    </el-form-item>
    <el-form-item label="企业类型" prop="type">
      <el-select clearable v-model="queryParams.type" placeholder="请选择企业类型" class="!w-200px">
        <el-option v-for="dict in getIntDictOptions(DICT_TYPE.ENTER_TYPE)" :key="dict.value" :label="dict.label"
          :value="dict.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="企业属性" prop="companyAttribute">
      <el-select clearable v-model="queryParams.companyAttribute" placeholder="请选择企业属性" class="!w-200px">
        <el-option v-for="dict in getIntDictOptions(DICT_TYPE.COMPANY_ATTRIBUTE)" :key="dict.value" :label="dict.label"
          :value="dict.value" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleQuery">搜索</el-button>
      <el-button @click="resetQuery">重置</el-button>
    </el-form-item>
  </el-form>
  <div class="flex justify-between py-12px">
    <div>
      <el-button type="primary" @click="openForm()" v-hasPermi="['mst:enterpriseManage:create']">新增</el-button>
    </div>
  </div>
  <el-table v-loading="loading" :data="list" border stripe
    :header-cell-style="{ background: '#F3F4F7', color: '#555' }">
    <el-table-column label="企业名称" align="center" prop="tenantName" min-width="200" fixed="left"
      :show-overflow-tooltip="true" />
    <el-table-column label="企业简称" align="center" prop="shortName" min-width="150" :show-overflow-tooltip="true" />
    <el-table-column label="行政区划" align="center" prop="administrativeDivision" min-width="200"
      :show-overflow-tooltip="true">
      <template #default="{ row }">
        <span>
          {{ getDictLabel(DICT_TYPE.ADMINISTRATIVE_DIVISION, row.administrativeDivision?.toString()) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="企业类别" align="center" prop="type" width="150" :show-overflow-tooltip="true">
      <template #default="{ row }">
        <span>
          {{ getDictLabel(DICT_TYPE.ENTER_TYPE, row.type?.toString()) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="社会信用代码" align="center" prop="socialCreditCode" width="150" :show-overflow-tooltip="true" />
    <el-table-column label="联系人" align="center" prop="contactPerson" width="100" :show-overflow-tooltip="true" />
    <el-table-column label="联系电话" align="center" prop="contact" min-width="150" :show-overflow-tooltip="true" />
    <el-table-column label="行政地址" align="center" prop="address" width="250" :show-overflow-tooltip="true" />
    <el-table-column label="经纬度" align="center" prop="lngLat" width="200" :show-overflow-tooltip="true">
      <template #default="{ row }">
        <span v-if="row.longitude && row.latitude">
          {{ row.longitude }},{{ row.latitude }}</span>
      </template>
    </el-table-column>
    <el-table-column label="三方ID" align="center" prop="tenantId" width="100" :show-overflow-tooltip="true" />
    <el-table-column label="总产能" align="center" prop="capacity" width="100" :show-overflow-tooltip="true" />
    <el-table-column label="企业属性" align="center" prop="companyAttributeName" width="100"
      :show-overflow-tooltip="true" />
    <el-table-column label="数据来源" align="center" prop="platformSource" width="100" :show-overflow-tooltip="true">
      <template #default="{ row }">
        <span>
          {{ getDictLabel(DICT_TYPE.PLATFORM_SOURCE, row.platformSource?.toString()) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="股权类别" align="center" prop="stockType" width="100" :show-overflow-tooltip="true">
      <template #default="{ row }">
        <span>
          {{ getDictLabel(DICT_TYPE.STOCK_TYPE, row.stockType?.toString()) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="创建时间" align="center" prop="createTime" width="200" :formatter="dateFormatter"
      :show-overflow-tooltip="true" />
    <el-table-column label="修改时间" align="center" prop="updateTime" width="200" :formatter="dateFormatter"
      :show-overflow-tooltip="true" />
    <el-table-column label="操作" align="center" prop="operation" width="100" fixed="right">
      <template #default="{ row }">
        <el-button link type="primary" @click="openForm(row.id)" v-hasPermi="['mst:enterpriseManage:update']">
          编辑
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
    @pagination="getList" />
  <inputForm ref="inputFormRef" @success="resetQuery"></inputForm>
</template>
<script setup lang="ts" name="enterpriseManage">
  import * as Utils from '@/utils/dict'
  import * as coalMarket from '@/api/coalMarket'
  import inputForm from './inputForm.vue'
  import { dateFormatter } from '@/utils/formatTime'

  const { DICT_TYPE, getIntDictOptions, getDictLabel } = reactive(Utils)
  const loading = ref(true) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const list = ref([]) // 列表的数据
  const queryParams = ref({
    pageNo: 1,
    pageSize: 10,
    tenantName: undefined,
    type: undefined,
    companyAttribute: undefined
  })
  const queryFormRef = ref()
  const inputFormRef = ref()

  /** 查询列表 */

  const getList = async () => {
    loading.value = true
    try {
      const data = await coalMarket.getCompanyPage(queryParams.value)
      list.value = data.list || []
      total.value = data.total || 0
    } finally {
      loading.value = false
    }
  }

  const openForm = (id) => {
    inputFormRef.value.open(id)
  }

  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.value.pageNo = 1
    getList()
  }

  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
  }

  /** 初始化 **/
  onMounted(() => {
    getList()
  })

  defineExpose({})
</script>